/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eit.ntc.lmsbackend.struts.action;

import com.eit.ntc.lmsbackend.struts.form.CatalogForm;
import com.eit.ntc.struts.action.AbstractWebFlowAction;
import com.eit.toschoolonline.lms.jdo.CatalogM;
import com.eit.toschoolonline.lms.jdo.LearningItemM;
import com.eit.toschoolonline.lms.service.LearningItemService;
import com.eit.toschoolonline.lms.service.LmsServiceFactory;
import com.eit.toschoolonline.lms.service.spring.CatalogServiceSpringBean;
import java.util.ArrayList;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
import java.util.List;
import java.util.Set;
import org.springframework.validation.Errors;

/**
 *
 * @author tawatchai
 */
public class CatalogAction extends AbstractWebFlowAction {

    private LmsServiceFactory lmsServiceFactory;

    public LmsServiceFactory getLmsServiceFactory() {
        return lmsServiceFactory;
    }

    public void setLmsServiceFactory(LmsServiceFactory lmsServiceFactory) {
        this.lmsServiceFactory = lmsServiceFactory;
    }

    public Event deleteCatalog(RequestContext context) throws Exception {
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        Errors errors = getFormErrors(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        String catalogCode = getHttpServletRequestParameter(context, "catalog");

        CatalogM catalog = service.getCatalogs(catalogCode);



        if (catalog != null) {
            CatalogM upperCatalog = service.getUpperCatalog(catalog.getCatalogCode());
            if (catalog.getLowerCatalog().isEmpty()) {

                service.deleteCatalog(upperCatalog, catalog);
            } else {
                errors.reject("errors.CatalogForm.NotEmptyLowerCatalog");
                return error();
            }
            ;
        } else {
            errors.reject("errors.CatalogForm.NotFoundCatalog");
            return error();

        }
        return success();
    }

    public Event getRootCatalogs(RequestContext context) throws Exception {
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();

        cForm.setUpperCatalog(null);
        List catalogs = service.getCatalogs();
        cForm.setCatalogs(catalogs);
        cForm.setLevelCatalogs(new ArrayList());
        cForm.setCatalogDesc("");
        cForm.setCatalogName("");

        return success();
    }

    public Event getLearningItem(RequestContext context) throws Exception {
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        cForm.setItemCode("");
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        CatalogM catalog = null;
        String cId = getHttpServletRequestParameter(context, "catalog");
        if (cId != null && !cId.trim().equals("")) {
            catalog = service.getLearningItems(cId);


        } else {
            CatalogM sCatalog = cForm.getSelectCatalog();
            if (sCatalog != null && sCatalog.getCatalogCode() != null && !sCatalog.getCatalogCode().trim().equals("")) {
                catalog = service.getLearningItems(sCatalog.getCatalogCode());
            }
            ;
        }
        ;
        if (catalog != null) {
            List items = new ArrayList(catalog.getLearningItems());
            cForm.setLearningItems(items);
            cForm.setSelectCatalog(catalog);
            return success();
        }
        return new Event(this, "notFoundCatalog");
    }

    public Event addLearningItem(RequestContext context) throws Exception {
        Errors errors = getFormErrors(context);
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        LearningItemService itemService = lmsServiceFactory.getLearningItemService();
        String itemCode = cForm.getItemCode();
        if (itemCode != null && !itemCode.trim().equals("")) {
            int itemCodeInt = Integer.parseInt(itemCode);
            LearningItemM item = itemService.getLearningItemByCode(itemCodeInt);
            if (item != null) {
                CatalogM catalog = cForm.getSelectCatalog();
                if (!catalog.getLearningItems().contains(item)) {
                    catalog.getLearningItems().add(item);
                    service.updateCatalog(catalog);
                    return success();
                } else {
                    errors.reject("CatalogForm.duplicateLearningItem", "duplicate Learning Item");
                    return new Event(this, "notFoundItem");
                }
            }

        }
        ;

        errors.reject("CatalogForm.NotFoundLearningItem", "Not Found Learning Item");
        return new Event(this, "notFoundItem");
    }

    public Event addLearningItemList(RequestContext context) throws Exception {
        Errors errors = getFormErrors(context);
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        LearningItemService itemService = lmsServiceFactory.getLearningItemService();
        String username = getRemoteUser(context);
        String catalogCode = cForm.getSelectCatalog().getCatalogCode();
        // for normal uesr
        // List addItems = itemService.getAvailableItems(username,catalogCode);

        // for admin user;
        List addItems = itemService.getAvailableItems(catalogCode);
        cForm.setAddItems(addItems);
        cForm.setAddedItemIndexs(null);
        //Set catalogs = cForm.getSelectCatalog().getLearningItems();
        return success();
    }

    public Event saveAddLearningItemList(RequestContext context) throws Exception {
        Errors errors = getFormErrors(context);
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        LearningItemService itemService = lmsServiceFactory.getLearningItemService();

        String[] itemIndexs = cForm.getAddedItemIndexs();
        CatalogM catalog = cForm.getSelectCatalog();
        if (itemIndexs != null && itemIndexs.length > 0) {
            for (int i = 0; i < itemIndexs.length; i++) {
                String itemIndexStr = itemIndexs[i];
                int itemIndexInt = Integer.parseInt(itemIndexStr);
                LearningItemM addItem = (LearningItemM) cForm.getAddItems().get(itemIndexInt);
                if (addItem != null) {
                    catalog.getLearningItems().add(addItem);
                }
            }
            service.updateCatalog(catalog);
            return success();
        }
        //Set catalogs = cForm.getSelectCatalog().getLearningItems();

        errors.reject("catalog.emptyChkbox");
        return error();
    }

    public Event removeLearningItem(RequestContext context) throws Exception {
        Errors errors = getFormErrors(context);
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        LearningItemService itemService = lmsServiceFactory.getLearningItemService();
        String itemIndex = getHttpServletRequestParameter(context, "index");
        if (itemIndex != null && !itemIndex.trim().equals("")) {
            int itemIndexInt = Integer.parseInt(itemIndex);
            CatalogM catalog = cForm.getSelectCatalog();
//              List itemList = new ArrayList(cForm.getLearningItems());
            LearningItemM delItem = (LearningItemM) cForm.getLearningItems().get(itemIndexInt);
            System.out.println("select from Form");
            catalog.getLearningItems().remove(delItem);
            service.updateCatalog(catalog);
            return success();

//            LearningItemM item = itemService.getLearningItemByCode(itemCodeInt);
//            if (item!=null){
//                CatalogM catalog = cForm.getSelectCatalog();
//                if (!catalog.getLearningItems().contains(item)){
//                    catalog.getLearningItems().add(item);
//                    service.updateCatalog(catalog);
//                    return success();
//                }else {
//                    errors.reject("CatalogForm.duplicateLearningItem","duplicate Learning Item");
//                    return new Event(this,"notFoundItem");
//                }
//            }

        }
        ;

        errors.reject("CatalogForm.NotFoundLearningItem", "Not Found Learning Item");
        return new Event(this, "notFoundItem");
    }

    public Event editCatalog(RequestContext context) throws Exception {
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        String cId = getHttpServletRequestParameter(context, "catalog");
        if (cId != null && !cId.trim().equals("")) {
            CatalogM catalog = service.getCatalogs(cId);
            if (catalog != null) {
                cForm.setSelectCatalog(catalog);
                cForm.setCatalogDesc(catalog.getDescription());
                cForm.setCatalogName(catalog.getName());
                return success();
            }
        }
        ;
        return new Event(this, "notFoundCatalog");
    }

    public Event updateCatalog(RequestContext context) throws Exception {
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        CatalogM catalog = cForm.getSelectCatalog();
        if (catalog != null) {
            catalog.setName(cForm.getCatalogName());
            catalog.setDescription(cForm.getCatalogDesc());
            service.updateCatalog(catalog);
            return success();
        }
        return new Event(this, "notFoundCatalog");
    }

    public Event getCatalogs(RequestContext context) throws Exception {
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        CatalogM catalog = null;
        String cId = getHttpServletRequestParameter(context, "catalog");
        if (cId != null && !cId.trim().equals("")) {
            catalog = service.getCatalogs(cId);
            if (catalog == null) {
                return new Event(this, "notFoundCatalog");
            }

        } else {
            CatalogM upperCatalog = cForm.getUpperCatalog();
            if (upperCatalog != null) {
                catalog = service.getCatalogs(upperCatalog.getCatalogCode());


            } else {
                return new Event(this, "getRootCatalog");
            }
            ;
        }
        List levelCatalogList = service.getLevelCatalogs(catalog.getCatalogCode());

        cForm.setUpperCatalog(catalog);
        cForm.setCatalogs(new ArrayList(catalog.getLowerCatalog()));
        cForm.setLevelCatalogs(levelCatalogList);
        cForm.setCatalogDesc("");
        cForm.setCatalogName("");

        return success();
    }

    public Event addCatalog(RequestContext context) throws Exception {
        CatalogForm cForm = (CatalogForm) getFormObject(context);
        CatalogServiceSpringBean service = lmsServiceFactory.getCatalogServiceSpringBean();
        CatalogM newCatalog = new CatalogM();

        newCatalog.setDescription(cForm.getCatalogDesc());
        newCatalog.setName(cForm.getCatalogName());

        service.addCatalog(newCatalog, cForm.getUpperCatalog());

        return success();
    }
}
